<template>
	<view class="quick-diag">		
		<view class="content">
			<!-- 开头 -->
			<view class="diag_title" v-if="dataobj.id">
				<view class="head u-flex u-flex-between">
					<view>{{dataobj.facestatus==1?'已通过':'未通过'}}实名认证</view>
					<view>执业证书编号：{{dataobj.certificate_code}}</view>
				</view>
				<view class="userinfo u-flex">
					<image class="avatar" :src="$base.cdn(dataobj.img,'/static/lj/common/default-doctor.png')" mode="aspectFill"></image>
					<view>
						<view class="tit u-flex u-flex-items-start">
							<view class="u-flex-fill u-flex u-flex-items-end">
								<view class="name">{{dataobj.name}}</view>
								<view class="zc">{{dataobj.academic_title||'暂无职称'}}</view>
							</view>
						</view>
						<view>
							<uni-rate :size="12" activeColor="#ea9518" allow-half :readonly="true" :value="dataobj.score" />
						</view>
						<view class="con">
							擅长：{{dataobj.be_good_at_synopsis||'暂无'}}
							<text class="btn-text-detail" @click="goNext('/pages/info/docinfo?id=' + id,false)">详情</text>
						</view>
					</view>
				</view>
			</view>
			

			<!-- 就诊人 -->
			<view class="card">
				<view class="title">
					选择就诊人
					<!-- <text class="ComPopup_chooisa font-989898 font14" v-if="familyobj.the_patient_info">({{familyobj.the_patient_info.name?familyobj.the_patient_info.name+' 卡号:'+familyobj.the_patient_info.code:'暂无'}})</text> -->
					<!-- <text class="ComPopup_chooisa font-989898 font14" v-if="familyobj.the_patient_info">({{familyobj.the_patient_info.name?familyobj.the_patient_info.name:'暂无'}})</text> -->
				</view>
				<view class="seeadoctor">
					<view class="ComPopup_list">
						<view class="ComPopup_listname" :class="item.id==familyobj.id?'listname_a':'listname_b'"
							v-for="(item,index) in familyList" :key="index" @click="Onchooisfamily(item)">
							{{item.the_patient_info&&item.the_patient_info.namea?item.the_patient_info.namea:'--'}}
						</view>
						<view class="ComPopup_listadd" @click="Onjzrgladd">+</view>
					</view>
				</view>
				
				<view class="title">本次咨询目的</view>
				<view class="objective">
					<view class="item" :class="{on:item.show}" @click="Oncausechioess(item)" v-for="(item,index) in causeList" :key="index">{{item.name}}</view>
				</view>
				
				<view class="title">病情描述</view>
				<view class="describe">
					<textarea @input="Ondescribe" placeholder-class="placeholderClass" class="describe_textarea"
						placeholder="请描述您的病情症状和就诊等信息。(最少10个字)" />
				</view>
				
				<view class="title">上传检查报告及患处照片（最多9张）</view>
				<view class="imgList">
					<u-upload
						:fileList="uploadList"
						name="uploadImg"
						multiple
						:maxCount="9"
						width="126rpx"
						height="126rpx"
						uploadText="上传照片"
						@afterRead="afterRead"
						@delete="deletePic"
					></u-upload>
				</view>
			</view>
			
			<view class="btn-row">
				<view class="btn-fillet" @click="Onadd">提交问诊</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				id: 0,
				dataobj: {}, //医生详情
				typename: '', //类型
				familyList: [], //就诊人列表
				familyobj: {}, //默认就诊人
				causeList: [], //咨询目的列表
				cause: '', //咨询目的
				describe: '', //病情说明
				uploadList: [], //图片
				options: '', //类型
				timetext: '', //预约时间
				day: '', //日期
				week: '', //周几
				begin: '', //开始时间
				end: '', //结束时间
				causeID: null,
				
				family_id: 0, //就诊人id，用于复诊
			}
		},
		onLoad: function(options) {
			this.typename = options.type
			if(options.family_id) this.family_id = options.family_id
			
			if (options.type == 'sp' && options.day) {
				this.timetext = options.day + ' ' + options.week + ' ' + options.begin + ' ' + options.end
				this.day = options.day
				this.week = options.week
				this.begin = options.begin
				this.end = options.end
			}
			if(options.id){
				this.id = options.id

				uni.setNavigationBarTitle({
					title: this.typename=='tw'?"图文咨询":"视频咨询"
				})
				
				//医生详情
				this.Ondoctordetails()
			}
			//咨询目的列表
			this.Oncause()
		},
		onShow(){
			//获取就诊人
			this.Onlistmemberfamily()
		},
		methods: {
			// 删除图片
			deletePic(event) {
				this.uploadList.splice(event.index, 1)
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let uploadListLen = this.uploadList.length
				lists.map((item) => {
					this.uploadList.push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					let item = this.uploadList[uploadListLen]
					this.uploadList.splice(uploadListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					uploadListLen++
				}
			},
			uploadFilePromise(url) {
				var that = this
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: that.$Config.http_static+'api/common/uploader',
						filePath: url,
						name: 'file',
						formData: {},
						success: (uploadFileRes) => {
							var res = JSON.parse(uploadFileRes.data);
							console.log('uploadFilePromise',res)
							resolve(res.data.fullurl)
						},
						fail: e => {
							resolve({code:0})
						},
					});
				})
			},
			
			//详情
			Ondoctordetails() {
				this.$Http({
					url: 'doctorDetails',
					data: {
						doctor_id: this.id,
					}
				}).then(res => {
					this.dataobj = res.data
					if(this.dataobj.be_good_at_synopsis.length>30){
						this.dataobj.be_good_at_synopsis = this.dataobj.be_good_at_synopsis.substr(0, 30)+'......'
					}
				})
			},
			//获取就诊人
			Onlistmemberfamily() {
				this.$Http({
					url: 'listThePatient',
					data: {}
				}).then(res => {
					if (!res.data || res.data.length == 0) {} else {
						this.familyList = res.data
						
						var default_familyobj = res.data[0] ? res.data[0] : {};
						var default_familyobj2 = null;
						
						res.data.forEach(item => {
							item.the_patient_info.namea = item.the_patient_info.name.substring(item
								.the_patient_info.name.length - 2)
							if (item.isdefault == 1) {
								default_familyobj = item
							}
								
							if(
								this.family_id && this.family_id!=0 &&
								this.family_id==item.the_patient_id
							){
								default_familyobj2 = item
							}
						})
						
						if(!this.familyobj || !this.familyobj.id) this.familyobj = default_familyobj2!=null?default_familyobj2:default_familyobj
					}
				})
			},
			//选择就诊人
			Onchooisfamily(e) {
				console.log('Onchooisfamily',e)
				this.familyobj = e
				this.$forceUpdate()
			},
			//添加就诊人
			Onjzrgladd() {
				this.$store.commit('Onjzkurl', '/pagesL/quick/diag?id=' + this.id + '&type=' + this.typename)
				uni.navigateTo({
					url: '/pagesA/my/jzrgladd',
				})
			},
			//咨询目的
			Oncause() {
				this.$Http({
					url: 'consultationConsult',
					data: {}
				}).then(res => {
					if (res.code == 1) {
						res.data.consultation_consult.forEach(item => {
							item.show = false
						})
						if(this.family_id){
							res.data.consultation_consult[1].show = true
							this.cause = res.data.consultation_consult[1].name
							this.causeID = res.data.consultation_consult[1].id
							this.causeList = res.data.consultation_consult
						}else{
							res.data.consultation_consult[0].show = true
							this.cause = res.data.consultation_consult[0].name
							this.causeID = res.data.consultation_consult[0].id
							this.causeList = res.data.consultation_consult
						}
							
					}
				})
			},
			//选择咨询目的
			Oncausechioess(e) {
				this.causeList.forEach(item => {
					item.show = false
					if (item.id == e.id) {
						item.show = true
					}
				})
				this.cause = e.name
				this.causeID = e.id
			},
			//病情说明
			Ondescribe(e) {
				this.describe = e.detail.value
			},
			
			
			//提交
			Onadd() {
				let that = this
				if (this.familyobj.id == undefined) {
					uni.showToast({
						title: '请选择就诊人',
						icon: 'none',
						duration: 2000
					})
					return
				}
				if (this.describe == '' || this.describe.length < 10) {
					uni.showToast({
						title: '请填写病情描述并且至少10个字',
						icon: 'none',
						duration: 2000
					})
					return
				}
				let data = {
					doctor_id: this.id,
					consultation_consult_id: this.causeID,
					content: this.describe,
					the_patient_id: this.familyobj.the_patient_info?this.familyobj.the_patient_info.id:0,
				}
				let img = ''
				let url = ''
				if (this.uploadList.length > 0) {
					this.uploadList.forEach(item => {
						img = img + item.url + ','
					})
					data.images = img
				}

				if (this.typename == 'tw') {
					url = 'createConsultationOrdernew'
				}
				if (this.typename == 'sp') {
					url = 'createConsultationVideoOrdernew'
				}
				this.$Http({
					url: url,
					data: data,
					show: true,
					type: 'post'
				}).then(res => {
					if (res.code == 1) {
						if(res.data.pay_code){
							if (that.typename == 'sp') {
								uni.redirectTo({
									url: '/pagesA/my/videoList/videoList?type=1',
								})
							}
							if (that.typename == 'tw') {
								uni.redirectTo({
									url: '/pagesA/my/textList/textList?type=1',
								})
							}
						}else{
							uni.redirectTo({
								url: '/pagesA/ask/payment?id=' + res.data.order_code + '&type=' + this.typename,
							})
						}
					}
				})

			},
			
			// 路由跳转: tag默认false,tag=true跳到tabBar页面
			goNext(url, tag) {
				if (tag) {
					uni.switchTab({
						url: url
					});
				} else {
					uni.navigateTo({
						url: url
					});
				}
			},
		}
	}
</script>

<style lang="less">
	@import '@/pagesL/style.less';
	
	page{background-color: #F6F6F6;}
</style>
